package com.cloudeasy.label.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.label.LabelVO;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.label.dto.LabelDTO;
import com.cloudeasy.label.field.LabelField;
import com.cloudeasy.label.po.Label;
import com.cloudeasy.label.service.LabelService;
import com.cloudeasy.label.util.IPageUtil;
import com.cloudeasy.label.util.LabelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
public class LabelController {

    private final LabelService labelService;

    @Autowired
    public LabelController(LabelService labelService) {
        this.labelService = labelService;
    }

    /**
     * 添加标签
     *
     * @param labelDTO {@link LabelDTO}
     * @return {@link Result<Boolean>}
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody @Validated LabelDTO labelDTO) {
        return ResultUtil.success(labelService.save(labelDTO));
    }

    /**
     * 更新标签
     *
     * @param id       标签Id
     * @param labelDTO {@link LabelDTO}
     * @return {@link Result<Boolean>}
     */
    @PutMapping("/{id}")
    public Result<Boolean> update(@PathVariable("id") String id, @RequestBody @Validated LabelDTO labelDTO) {
        return ResultUtil.success(labelService.updateById(id, labelDTO));
    }

    /**
     * 删除标签
     *
     * @param id 标签Id
     * @return {@link Result<Boolean>}
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteByIds(@PathVariable("id") String id) {
        return ResultUtil.success(labelService.removeById(id));
    }

    /**
     * 查询标签
     *
     * @param ids      标签Id列表
     * @param names    标签名称列表
     * @param statuses 状态列表
     * @param asc      正序排序字段
     * @param desc     倒序排序字段
     * @return {@link List<LabelVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<LabelVO>> list(@RequestParam(value = "ids", required = false) List<String> ids,
                                      @RequestParam(value = "names", required = false) List<String> names,
                                      @RequestParam(value = "statuses", required = false) List<String> statuses,
                                      @RequestParam(value = "asc", required = false) String[] asc,
                                      @RequestParam(value = "desc", required = false) String[] desc) {
        QueryWrapper<Label> queryWrapper = LabelUtil.compose(ids, names, statuses, asc, desc);
        return ResultUtil.success(LabelUtil.convert(labelService.list(queryWrapper)));
    }

    /**
     * 分页查询标签
     *
     * @param ids      标签Id列表
     * @param names    标签名称列表
     * @param statuses 状态列表
     * @param asc      正序排序字段
     * @param desc     倒序排序字段
     * @param page     页数
     * @param size     每页大小
     * @return {@link PageResult<LabelVO>}的{@link Result}
     */
    @GetMapping("/{page}/{size}")
    public Result<PageResult<LabelVO>> pageQuery(@RequestParam(value = "ids", required = false) List<String> ids,
                                                 @RequestParam(value = "names", required = false) List<String> names,
                                                 @RequestParam(value = "statuses", required = false) List<String> statuses,
                                                 @RequestParam(value = "asc", required = false) String[] asc,
                                                 @RequestParam(value = "desc", required = false) String[] desc,
                                                 @PathVariable("page") Integer page,
                                                 @PathVariable("size") Integer size) {
        QueryWrapper<Label> queryWrapper = LabelUtil.compose(ids, names, statuses, asc, desc);
        IPage<Label> labelTempIPage = IPageUtil.compose(page, size);
        return ResultUtil.success(LabelUtil.convert(labelService.page(labelTempIPage, queryWrapper)));
    }
}
